package com.pktechan.api.dto;

import java.util.List;

/**
 * @program: cloudfloating
 * @description: 小程序订阅消息实体
 * @author: qx.zhang
 * @create: 2021-03-23 11:27
 **/
public class MiniSubscribeMessage {

    /**
     * 模板所属小程序
     */
    private String mpId;

    /**
     * 消息接收方
     */
    private String toUser;
    /**
     * 模板ID
     */
    private String templateId;
    /**
     * 点击模板卡片后的跳转页面，仅限本小程序内的页面。支持带参数,（示例index?foo=bar）。该字段不填则模板无跳转。
     */
    private String page;

    /**
     * 模板内容，格式形如 { "key1": { "value": any }, "key2": { "value": any } }
     */
    private String data;
    /**
     * 跳转小程序类型：developer为开发版；trial为体验版；formal为正式版；默认为正式版
     */
    private String miniProgramState;
    /**
     * 进入小程序查看”的语言类型，支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文)，默认为zh_CN
     */
    private String lang;

    /**
     * 订阅消息DATA
     */
    private List<MiniSubscribeMessageParam> miniSubscribeMessageParamList;

    public String getMpId() {
        return mpId;
    }

    public void setMpId(String mpId) {
        this.mpId = mpId;
    }

    public String getToUser() {
        return toUser;
    }

    public void setToUser(String toUser) {
        this.toUser = toUser;
    }

    public String getTemplateId() {
        return templateId;
    }

    public void setTemplateId(String templateId) {
        this.templateId = templateId;
    }

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getMiniProgramState() {
        return miniProgramState;
    }

    public void setMiniProgramState(String miniProgramState) {
        this.miniProgramState = miniProgramState;
    }

    public String getLang() {
        return lang;
    }

    public void setLang(String lang) {
        this.lang = lang;
    }

    public List<MiniSubscribeMessageParam> getMiniSubscribeMessageParamList() {
        return miniSubscribeMessageParamList;
    }

    public void setMiniSubscribeMessageParamList(List<MiniSubscribeMessageParam> miniSubscribeMessageParamList) {
        this.miniSubscribeMessageParamList = miniSubscribeMessageParamList;
    }

    public String toJSON() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("{");
        buffer.append(String.format("\"touser\":\"%s\"", this.toUser)).append(",");
        buffer.append(String.format("\"template_id\":\"%s\"", this.templateId)).append(",");
        buffer.append(String.format("\"page\":\"%s\"", this.page)).append(",");
        buffer.append("\"data\":{");
        MiniSubscribeMessageParam param = null;
        for (int i = 0; i < this.miniSubscribeMessageParamList.size(); i++) {
            param = miniSubscribeMessageParamList.get(i);
            // 判断是否追加逗号
            if (i < this.miniSubscribeMessageParamList.size() - 1) {

                buffer.append(String.format("\"%s\": {\"value\":\"%s\"},", param.getName(), param.getValue()));
            } else {
                buffer.append(String.format("\"%s\": {\"value\":\"%s\"}", param.getName(), param.getValue()));
            }

        }
        buffer.append("}");
        buffer.append("}");
        return buffer.toString();
    }
}
